    //
//  KDHttpTool.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/27.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDHttpTool.h"

@implementation KDHttpTool
+ (void) get:(NSString *)url parameters:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure;
{
    //获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    manager.requestSerializer.timeoutInterval = 15.0;//超时
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html",@"text/plain",@"text/javascript",@"application/json", @"text/json",@"application/force-download"]];
    KDAccount *account = [KDAccountTool account];
    [manager.requestSerializer setValue:account.token forHTTPHeaderField:@"token"];
    [manager GET:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        KDLog(@"%@", task.currentRequest.URL);
        KDLog(@"%@", responseObject);
        
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        KDLog(@"operation: %@" , task.currentRequest.URL );
        failure(error);
    }];

}
/**
 *  正常的post
 */
+ (void) post:(NSString *)url parameters:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure
{
    //获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30.0;
//    KDAccount *account =[KDAccountTool account];
//    [manager.requestSerializer setValue:account.cookie forHTTPHeaderField:@"Set-Cookie"];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    KDAccount *account = [KDAccountTool account];
    [manager.requestSerializer setValue:account.token forHTTPHeaderField:@"token"];
     //KDLog(@"%@", manager.requestSerializer.HTTPRequestHeaders);
//    [manager.requestSerializer setValue:@"text/html;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",@"text/json", @"text/plain", @"text/html"]];
    [manager POST:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        KDLog(@"%@", task.currentRequest.URL);
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 *  将json放到body里的那种post
 */
+ (void) post:(NSString *)url bodyJson:(id)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure
{
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",@"text/json", @"text/plain", @"text/html"]];
    manager.responseSerializer = responseSerializer;
    KDAccount *account = [KDAccountTool account];
    [requestSerializer setValue:account.token forHTTPHeaderField:@"token"];
    requestSerializer.timeoutInterval = 15.0;//超时
    NSMutableURLRequest *req = [requestSerializer requestWithMethod:@"POST" URLString:url parameters:nil error:nil];
    NSError *error1;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
                                                       options:0
                                                         error:&error1];
    req.timeoutInterval= 30.0;
    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setHTTPBody:jsonData];
    
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        if (!error) {
            //NSLog(@"Reply JSON: %@", responseObject);
            KDLog(@"%@", responseObject);
            success(responseObject);
        } else {
            KDLog(@"Error: %@, %@, %@", error, response, responseObject);
            failure(error);
        }
    }] resume];
}
+ (void) post:(NSString *)url parameters:(NSDictionary *)params constructingBodyWithBlock:(void(^)(id<AFMultipartFormData> formData))imageblock success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure
{
    //获得请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        imageblock(formData);
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}

+ (void) put:(NSString *)url parameters:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure
{
    //获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30.0;
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    KDAccount *account = [KDAccountTool account];
    [manager.requestSerializer setValue:account.token forHTTPHeaderField:@"token"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",@"text/json", @"text/plain", @"text/html"]];

    [manager  PUT:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        KDLog(@"%@", task.currentRequest.URL);
        success(responseObject);

    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        failure(error);
    }];

}

@end
